// url:请求地址  type:请求方式  data:请求参数
var baseUrl = "http://zhaopin-api.weihankeji.com/api/";

function noop() {}
var noAuthURLList = ['/01.html', '/04.html', '/27.html', '/28.html', '/52.html']

function request(url, type, data, func, err) {
  $.ajax({
    url: baseUrl + url,
    type: type,
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    data: data,
    beforeSend: function(XMLHttpRequest) {
      XMLHttpRequest.setRequestHeader("Authorization", getToken() || null);
      XMLHttpRequest.setRequestHeader("Accept", "application/json");
    },
    success: function(res, status, xhr) {

      var token = xhr.getResponseHeader("authorization");
      if (token) {
        setToken(token);
      }
      if (
        xhr.status == 204 ||
        xhr.status == 201 ||
        xhr.status == 200 ||
        xhr.status == 202
      ) {
        if (res) {
          if (res.code == 1 || res.code == 200) {
            func && func(res);
          } else {
            err && err(res);
          }
        } else {
          func && func({
            code: 1,
            message: "操作成功"
          });
        }
      } else {
        err && err(res);
      }
    },
    error: function(res) {
      if (res.status === 401 && !~noAuthURLList.indexOf(location.pathname)) {
        return location.href = './01.html'
      }
      err && err(res);
    }
  });
}

function login(options) {
  var isEnterprise = !options.isUser;
  var success = options.success || noop;
  var fail = options.fail || noop;
  var url =
    options.url ||
    (options.isUser ? "person/user/login" : "enterprise/user/login");
  request(
    url,
    "POST",
    options.data,
    function(res) {
      success(res);
      setTimeout(function() {
        if (isEnterprise) {
          var data = res.data;
          var enterprise_apply = data.enterprise_apply;
          if (enterprise_apply) {
            location.href = +enterprise_apply.status === 1 ? "./50.html" : './53.html';
          } else {
            location.href = "./52.html";
          }
        } else {
          location.href = "./06.html";
        }
      }, 800);
    },
    fail
  );
}

function getToken() {
  return sessionStorage.getItem("token") || "";
}

function setToken(data) {
  sessionStorage.setItem("token", data);
}

var qiniuUrl = "http://changhai-img.weihankeji.com";
var getUploadToken = (function() {
  var _uploadToken = null;

  return function(callback, failCallback) {
    if (_uploadToken) return callback(_uploadToken);
    request(
      "common/qiniu/token",
      "GET",
      "",
      function(res) {
        _uploadToken = res.data.token;
        callback(_uploadToken);
      },
      failCallback
    );
  };
})();

function uploadImg(options) {
  // option  = {
  // 	file: File,
  // 	success: Function,
  // 	fail: Function,
  // 	complete: Function,
  // }
  getUploadToken(function(uploadToken) {
    var formData = new FormData();
    formData.append("token", uploadToken);
    formData.append("file", options.file);
    $.ajax({
      type: "POST",
      url: "https://upload-z2.qiniup.com",
      contentType: false,
      data: formData,
      processData: false,
      dataType: "json",
      success: function(r) {
        options.success(r);
      },
      error: function(err) {
        console.log("upload img fail", err);
      },
      complete: options.complete || null
    });
  }, options.fail);
}

// 使用前先引入插件
//  <script src="js/jquery-ajax-blob-arraybuffer.js"></script>
function downloadExport(url, name) {
  $.ajax({
    url: url,
    type: "GET",
    dataType: 'blob',
    headers: {
      Authorization: getToken(),
      Accept: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    },
    success: function(data, status, res) {
      var blob = data
      if (window.navigator.msSaveOrOpenBlob) {
        navigator.msSaveBlob(blob);
      } else {
        var elink = document.createElement('a');
        elink.download = name + ".xlsx";
        elink.style.display = 'none';
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        document.body.removeChild(elink); // 移除
      }
    }
  })
}